package custom.intents;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.widget.Toast;

import java.io.File;

import pri.zey.custom.tools.TBaseInfo;
import pri.zey.custom.tools.TPath;
import pri.zey.custom.tools.TStrKit;


/**
 * Created by Administrator on 2016/1/7.
 *
 */
public class intent_PickCamera {

    private static Context context;
    public File cameraFile;

    public intent_PickCamera(Context context){
        this.context = context;
    }

    /**
     * 照相获取图片
     */
    public void selectPicFromCamera() {
        if (!TBaseInfo.isExitsSdcard()) {
//            Toast.makeText(context, constant.sd_card_does_not_exist, Toast.LENGTH_LONG).show();
            return;
        }

        cameraFile = new File(TPath.getInstance().getImagePath(), System.currentTimeMillis() + ".jpg");
        cameraFile.getParentFile().mkdirs();
        ((Activity) context).startActivityForResult(
                new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile)),
                4000);
    }

    /**
     * 从图库获取图片
     */
    public void selectPicFromLocal() {
        Intent intent = null;
        if (Build.VERSION.SDK_INT < 19) {
            intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");

        } else {
            intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        }
        ((Activity) context).startActivityForResult(intent, 5000);
    }

    public String getLocalPhotoPath(Intent data) {
        if (data != null) {
            Uri selectedImage = data.getData();
            if (selectedImage != null) {
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                if (cursor != null) {
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    cursor = null;

                    if (picturePath == null || picturePath.equals("null")) {
//                        u_Msg.Toastl(context, "Can't find pictures");
                        return "Can't find pictures";
                    }
                    return picturePath;
                } else {
                    File file = new File(selectedImage.getPath());
                    if (!file.exists()) {
//                        u_Msg.Toastl(context, "Can't find pictures");
                        return "Can't find pictures";
                    }
                    return file.getAbsolutePath();
                }
            }
        }
        return "Can't find pictures";
    }
}
